package jemuillot.BiohazardPanelPuzzle;

import jemuillot.pkg.Utilities.AfterTaste;
import jemuillot.pkg.Utilities.LocalizedPath;
import jemuillot.pkg.Utilities.PackApp;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.util.HorizontalAlign;

import android.view.KeyEvent;

public class OptionsScene extends BiohazardPanelPuzzleScene {

	static final int LAYER_TITLE = LAYER_BACKGROUND + 1;
	static final int LAYER_SELECTIONS = LAYER_TITLE + 1;

	static final int LAYERS = LAYER_SELECTIONS + 1;
	private Text headText;

	private AfterTaste afterTaste;

	public OptionsScene(final BiohazardPanelPuzzle bpp) {
		super(bpp, LAYERS);

		afterTaste = new AfterTaste(bpp);

		scene.setBackgroundEnabled(false);

		scene.getLayer(LAYER_BACKGROUND).addEntity(
				new Sprite(0, 0, bpp.backgroundRegion));

		headText = new Text(0, 0, bpp.font, getString(R.string.options),
				HorizontalAlign.CENTER);

		headText.setPosition(getCenterX(headText), OPTION_Y);

		scene.getLayer(LAYER_TITLE).addEntity(headText);

		Text afterTasteText = new Text(0, 0, bpp.font,
				bpp.getString(R.string.afterTaste), HorizontalAlign.CENTER) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {

					afterTaste.setDefaultDownloadUrl(String.format(downloadUrl,
							PackApp.getAppVersionName(bpp)));

					afterTaste.setDefaultHomepage(new LocalizedPath(
							homepageUrl, LocalizedPath.GOOGLECODE_WIKI, null,
							null).createLocalizedUrl());

					afterTaste.showRecommendedChoices();
				}
				return true;
			}
		};

		afterTasteText.setPosition(getCenterX(afterTasteText),
				AFTERTASTE_TEXT_Y);

		scene.getLayer(LAYER_SELECTIONS).addEntity(afterTasteText);

		scene.registerTouchArea(afterTasteText);

		Text checkUpdateText = new Text(0, 0, bpp.font,
				bpp.getString(R.string.checkUpdate), HorizontalAlign.CENTER) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
					bpp.updater
							.setNoUpdateNotifier(bpp.updater.defaultNoUpdateNotifier);
					bpp.updater.showSetup();
				}
				return true;
			}
		};

		checkUpdateText.setPosition(getCenterX(checkUpdateText),
				CHECK_UPDATE_TEXT_Y);

		scene.getLayer(LAYER_SELECTIONS).addEntity(checkUpdateText);

		scene.registerTouchArea(checkUpdateText);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			toMainMenu();
			return true;
		} else
			return super.onKeyDown(keyCode, event);
	}

	private void toMainMenu() {
		new MainMenuScene(bpp);
	}

}
